<?php

	namespace app\home\controller;

	use app\home\validate\AddressValidate;
	use think\Controller;
	use think\Request;
	use app\home\model\Address as addressModel;

	class Address extends Base
	{


		public function add () {
			$addressValidate = new AddressValidate();
			$validateRes = $addressValidate->goSceneCheck('add');
			if ($validateRes) {
				return error($validateRes);
			}
			$openid = $this->member->openid;
			$addressModel = new addressModel();
			//检测是否有默认地址
			$default = $this->request->post('default');
			$input = $this->request->post();
			$input['openid'] = $openid;
			$input['createtime'] = time();
			if ($default) {
				$addressModel->where(['openid' => $openid])->update(['default' => 0]);
				$addressModel->save($input);
			} else {
				$address_item = $addressModel->where(['openid' => $openid, 'default' => 1, 'deletetime' => 0])->find();
				if ($address_item) {
					$addressModel->save($input);
				} else {
					//子查询
					$item = $addressModel->where(['openid' => $openid, 'deletetime' => 0])->order('id asc')->select();
					if ($item) {
						$addressModel->where(['openid' => $openid, 'id' => $item[0]['id']])->update(['default' => 1]);
					}
					$addressModel->save($input);
				}
			}
			$id = $addressModel->id;
			if ($id) {
				return success(['address_id' => $addressModel->id]);
			}

			return error('地址增加失败');
		}


		public function getAddress () {
			$openid = $this->member->openid;
			$addressModel = new addressModel();
			$item = $addressModel->where(['openid' => $openid, 'default' => 1, 'deletetime' => 0])->find();

			return success($item);
		}

		public function getAddressList () {
			$openid = $this->member->openid;
			$addressModel = new addressModel();
			$list = $addressModel->where(['openid' => $openid, 'deletetime' => 0])->select();

			if (!empty($list)) {
				return success($list);
			}

			return error('暂无地址');
		}


		public function getAddressById () {
			$addressValidate = new AddressValidate();
			$validateRes = $addressValidate->goSceneCheck('getAddressById');
			if ($validateRes) {
				return error($validateRes);
			}

			$id = $this->request->post('address_id');
			$addressModel = new addressModel();
			$item = $addressModel->where(['id' => $id])->find();

			if ($item) {
				return success($item);
			}

			return error('地址不存在');
		}

		public function getArea () {
			$s = join("", file(ROOT_PATH . '/public/static/area/AreaNew.xml'));
			$array = XmlToArr($s);

			$newArr = [];
			if (is_array($array["province"])) {
				foreach ($array["province"] as $i => $v) {
					if (0 < $i) {
						$province = ["name" => $v["@attributes"]["name"], "code" => $v["@attributes"]["code"], "city" => []];
						if (is_array($v["city"])) {
							if (!isset($v["city"][0])) {
								$v["city"] = [$v["city"]];
							}
							foreach ($v["city"] as $ii => $vv) {
								$city = ["name" => $vv["@attributes"]["name"], "code" => $vv["@attributes"]["code"], "area" => []];
								if (is_array($vv["county"])) {
									if (!isset($vv["county"][0])) {
										$vv["county"] = [$vv["county"]];
									}
									foreach ($vv["county"] as $iii => $vvv) {
										$area = ["name" => $vvv["@attributes"]["name"], "code" => $vvv["@attributes"]["code"]];
										$city["area"][] = $area;
									}
								}
								$province["city"][] = $city;
							}
						}
						$newArr[] = $province;
					}
				}
			}

			return success($newArr);
		}


	}
